package com.colabo.j2ee.web.core.util;

import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import com.colabo.j2ee.web.core.context.ContextUtil;
import com.colabo.j2ee.web.core.context.CoreInitInfo;
import com.colabo.j2ee.web.core.vo.SessionUserHolder;

/**
 * Util for processing resource bundle files
 * @author Anthony Do
 * @since 2009-11-3
 *
 */
public class PropertyUtil {
	
	public final static Locale DEFAULT_LOCALE = Locale.getDefault();
	
	// instanciation is forbidden
	private PropertyUtil() {
	}
	
	/**
	 * initialize the bundle file
	 * @param bundleFileName 
	 *					file name of the specified bundle
	 * @return  a proerties object contains the specified resource
	 */
	public static synchronized Properties init(String bundleFileName) {
		Properties props = new Properties();
		ResourceBundle bundle = ResourceBundle.getBundle(bundleFileName);
		Enumeration<String> keys = bundle.getKeys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			String value = bundle.getString(key);
			props.put(key, value);
		}
		bundle = null;
		return props;
	}
	
	/**
	 * retrieve a property value with given key from a system resource bundle <br>
	 * Note: used in directory or achived file
	 * 
	 * @param  key - to a certain resource item 
	 * @return the resource value with the given key
	 */
	public static String getPropertyByKey(String key) {
		// get the default conf directory where the resource file stores
		String fileDir = CoreInitInfo.getInstance().getMsgFileDirectory();
		String filepath = fileDir.replace('.', '/') + "/";
		
		Locale localeFromSession = null;
		try {
			localeFromSession = ((SessionUserHolder) ContextUtil.getBean(SessionUserHolder.BEAN_ID)).getLocale();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Locale locale = localeFromSession != null ? localeFromSession : DEFAULT_LOCALE;
		return ArchiveUtil.getMessageInJar(filepath, "msg-" + parserFileName(key) + "_" + locale, ".properties",
				key);
	}

	/**
	 * retrieve a property value with given key from a system resource bundle <br>
	 * Note: used in directory or achived file
	 * 
	 * @param  key - to a certain resource item 
	 * @param  args - parameters used to parse the message
	 * @return the resource value with the given key
	 */
	public static String getPropertyByKey(String msgkey, String[] args) {
		String fileDir = CoreInitInfo.getInstance().getMsgFileDirectory();
		String filepath = fileDir.replace('.', '/') + "/";

		// AOP hooking is prior than filter hooking
		Locale localeFromSession = null;
		try {
			localeFromSession = ((SessionUserHolder) ContextUtil.getBean(SessionUserHolder.BEAN_ID)).getLocale();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Locale locale = localeFromSession != null ? localeFromSession : DEFAULT_LOCALE;
		return ArchiveUtil.getMessageInJar(filepath, "msg-" + parserFileName(msgkey) + "_" + locale, ".properties",
				msgkey, args);
	}

	/**
	 * retrieve a property value with given key from a system resource bundle <br>
	 * Note: used in directory or achived file
	 * 
	 * @param  key - to a certain resource item 
	 * @param  args - parameters used to parse the message
	 * @param  locale - user locale information
	 * @return the resource value with the given key
	 */
	public static String getPropertyByKey(String msgkey, String[] args, Locale locale) {
		String fileDir = CoreInitInfo.getInstance().getMsgFileDirectory();
		String filepath = fileDir.replace('.', '/') + "/";

		return ArchiveUtil.getMessageInJar(filepath, "msg-" + parserFileName(msgkey) + "_"
				+ (locale != null ? locale.toString() : Locale.getDefault().toString()), ".properties", msgkey, args);
	}

	/**
	 * retrieve a property value with given key from a system resource bundle <br>
	 * Note: used in directory or achived file
	 * 
	 * @param  key - to a certain resource item 
	 * @param  locale - user locale information
	 * @return the resource value with the given key
	 */
	public static String getPropertyByKey(String msgkey, Locale locale) {
		String fileDir = CoreInitInfo.getInstance().getMsgFileDirectory();
		String filepath = fileDir.replace('.', '/') + "/";
		return ArchiveUtil.getMessageInJar(filepath, "msg-" + parserFileName(msgkey) + "_"
				+ (locale != null ? locale.toString() : Locale.getDefault().toString()), ".properties", msgkey);
	}
	
	/**
	 * According to the error code, retrieve the msg file name.
	 * For instance: E_APP_001, return 'app'
	 * @param errorCode - error code
	 * @return msg file name 
	 */
	protected static String parserFileName(String errorCode) {
		if (errorCode == null || errorCode.trim().length() == 0){
			return null;
		}
		String[] splits = errorCode.split("_");
		if (splits == null || splits.length < 3){
			return null;
		} else{
			return splits[1].toLowerCase();
		}
	}
}
